package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Patent.Patent;
import com.groupseven.backend.mapper.PatentMapper;
import com.groupseven.backend.mapper.ProgramMapper;
import com.groupseven.backend.services.PatentService;
import com.groupseven.backend.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class PatentServiceImpl implements PatentService {
    @Autowired
    PatentMapper patentMapper;
    @Autowired
    ProgramMapper programMapper;
    @Autowired
    UserService userService;
    public List<Patent> getallPatents(){
        return patentMapper.getallPatents();
    }

    public Patent getPatentByPatentNumber(String PatentNumber){
        return patentMapper.getPatentByPatentNumber(PatentNumber);
    }

    public int AddPatentInfo(Patent patent){
//        Program program;
//        program=null;
//        program.setProgramNumber(patent.ProgramNumber);
        if(patentMapper.getPatentByPatentNumber(patent.PatentNumber)!=null){
            return 0;
        }
//        else if(programMapper.selectByProgramNumber(program)==null){
//            return -1;
//        }
        else {
            return patentMapper.AddPatentInfo(patent);
        }
    }

    public int updatePatentInfo(Patent patent){
//        Program program=new Program();
//        program=null;
//        program.setProgramNumber(patent.ProgramNumber);
        if(patentMapper.getPatentByPatentNumber(patent.PatentNumber)==null){
            return 0;
        }
//        else if (programMapper.selectByProgramNumber(program)==null){
//            return -1;
//        }
        else if(userService.selectByUsername(patent.username)==null){
            return -2;
        }
        else {
            return patentMapper.updatePatentInfo(patent);
        }
    }

    public int deleteallPatents(){
        return patentMapper.deleteallPatents();
    }

    public int deletePatentByPatentNumber(String PatentNumber){
        if(patentMapper.getPatentByPatentNumber(PatentNumber)==null){
            return 0;
        }
        return patentMapper.deletePatentByPatentNumber(PatentNumber);
    }

    /////////////////////////////////////////////////////////////////////////
    public int mutildelPatents(List<String> PatentNumbers){
        return patentMapper.mutildelPatents(PatentNumbers);
    }

    public List<Patent> getlikePatentByPatentNumber(String PatentNumber){
        return patentMapper.getlikePatentByPatentNumber(PatentNumber);
    }

    public List<Patent> getlikePatentByPatentName(String PatentName){
        return patentMapper.getlikePatentByPatentName(PatentName);
    }
    public List<Patent> getlikePatentByusername(String username){
        return patentMapper.getlikePatentByusername(username);
    }

    public List<Patent> getlikePatentByPatentAllAuthors(String PatentAllAuthors){
        return patentMapper.getlikePatentByPatentAllAuthors(PatentAllAuthors);
    }
//////////////////////////////////////////////////////////////////////////

    /////////////////////////////////
    public List<Patent> searchqueryPatent(String query){
        return patentMapper.searchqueryPatent(query);
    }
    ///////////////////////////////

    //////////////////////////////////////////
    public List<Patent> searchuserqueryPatent (String query,String username){
        return patentMapper.searchuserqueryPatent(query,username);
    }
    //////////////////////////////////////

}
